<?php
namespace app\index\controller;

use app\index\model\DeptModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDiaryModel;
use app\index\model\EnginFoldModel;
use app\index\model\EnginJianModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginSpeedModel;
use app\index\model\EnginStampListModel;
use app\index\model\EnginStampModel;
use app\index\model\EnginXunListModel;
use app\index\model\EnginXunModel;
use app\index\model\EnginZhengListModel;
use app\index\model\EnginZhengModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\SpeedMonthListModel;
use app\index\model\SpeedMonthModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\UserModel;
use app\index\model\EnginVisaModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BianhaoService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\SceneService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Scene extends Common
{
    public function list_engin_visa(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['visa.deleted_user','=', 0];
        $where[] = ['visa.comid','=',$this->userinfo['comid']];
        $where[] = ['visa.isturn','=',1];
        $where[] = ['visa.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['visa.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['visa.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['visa.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['visatype']) && !empty($param['visatype'])){
            $where[] = ['visa.visatype','=', $param['visatype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['visa.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['visa.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['visatype'] = (new OptService())->get_base_info('scene_visatype');
        $data['is_visatype'] = isset($param['visatype']) ? $param['visatype'] : '';

        $data['data'] = EnginVisaModel::alias('visa')
            ->field('visa.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=visa.enginid')
            ->leftJoin('flow_bill bill','bill.id=visa.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_visa', 'visa'))
            ->order('visa.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['visatype_name'] = (new OptService())->get_base_showname('scene_visatype', $item['visatype']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_visa');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_visa(Request $request){
        $param = $request->post();
        $data = EnginVisaModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginVisaModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_visa', $data['id'], '项目签证');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_visa(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginVisaModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['visatype_name'] = (new OptService())->get_base_showname('scene_visatype', $data['data']['visatype']);
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['files'] = (new OptService())->get_file('engin_visa', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_chan(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['chan.deleted_user','=', 0];
        $where[] = ['chan.comid','=',$this->userinfo['comid']];
        $where[] = ['chan.isturn','=',1];
        $where[] = ['chan.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['chan.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chan.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['chan.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['chan.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['chan.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginChanModel::alias('chan')
            ->field('chan.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=chan.enginid')
            ->leftJoin('flow_bill bill','bill.id=chan.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_chan', 'chan'))
            ->order('chan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_chan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_chan(Request $request){
        $param = $request->post();
        $data = EnginChanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginChanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_chan', $data['id'], '产值月报');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_chan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginChanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['files'] = (new OptService())->get_file('engin_chan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_speed_zong(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zong.deleted_user','=', 0];
        $where[] = ['zong.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zong.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['zong.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['zong.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['zong.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['zong.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['zong.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['zong.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedZongModel::alias('zong')
            ->field('zong.*,e.name as enginname')
            ->join('engin e','e.id=zong.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_zong', 'zong'))
            ->order('zong.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wanchan'] = 0;
                $item['wanbi'] = 0;
                $item['findt'] = '';
                $item['jinbi'] = '';

                $zongList = SpeedZongListModel::where(['zongid'=>$item['id']])->select()->toArray();
                foreach ($zongList as $v){
                    $findt = EnginSpeedListModel::alias('list')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$v['id']])
                        ->value('list.findt');
                    if(!empty($findt)){
                        if(empty($item['findt'])){
                            $item['findt'] = $findt;
                        }else{
                            if($findt <= $item['findt']){
                                $item['findt'] = $findt;
                            }
                        }
                    }
                    $shinum = EnginSpeedListModel::alias('list')
                        ->join('engin_speed speed','speed.id=list.speedid')
                        ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                        ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$v['id']])
                        ->sum('list.shinum');
                    $item['wanchan'] += $shinum*$v['price'];
                }

                if($item['chanfee'] != 0){
                    $item['wanbi'] = round($item['wanchan'] / $item['chanfee'] *100, 2);
                }

                if(!empty($item['findt'])  && $item['findt'] != '0000-00-00'){
                    $jihua = get_time_slot_days($item['startdt'], $item['enddt']);
                    $shiji = get_time_slot_days($item['findt'], date("Y-m-d"));
                    if($jihua != 0){
                        $item['jinbi'] = round($shiji/$jihua*100, 2);
                    }
                }
                if(!$item['jinbi']){
                    $item['jinbi'] = 0;
                }


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('speed_zong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_speed_zong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入进度计划名称');
            }
            if(empty($param['startdt'])){
                return outputJson(4000, '请选择计划开始日期');
            }
            if(empty($param['enddt'])){
                return outputJson(4000, '请选择计划结束日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            $exist = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$param['enginid']])->find();
            if($exist){
                return outputJson(4000, '该项目总进度计划已存在');
            }


            $param['number'] = (new BianhaoService())->create_speed_zong_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'speed_zong') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }
            Db::startTrans();
            try {
                $id = SpeedZongModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zongid'] = $id;
                    SpeedZongListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_zong',$id);
                (new EnginRelevan())->vate_engin_file('speed_zong', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'speed_zong', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'speed_zong', $id, '总进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new BianhaoService())->create_speed_zong_number();
            $data['files'] = (new OptService())->get_file('speed_zong');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_speed_zong(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SpeedZongModel::where(['id'=>$param['id']])->find();


            if(empty($param['name'])){
                return outputJson(4000, '请输入进度计划名称');
            }
            if(empty($param['startdt'])){
                return outputJson(4000, '请选择计划开始日期');
            }
            if(empty($param['enddt'])){
                return outputJson(4000, '请选择计划结束日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            $exist = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$old['enginid']])
                ->where('id','neq',$old['id'])->find();
            if($exist){
                return outputJson(4000, '该项目总进度计划已存在');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'speed_zong') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }


            Db::startTrans();
            try {
                SpeedZongModel::update($param);

                SpeedZongListModel::where(['zongid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zongid'] = $param['id'];
                    SpeedZongListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_zong',$param['id']);
                (new EnginRelevan())->vate_engin_file('speed_zong', $param['id']);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'speed_zong', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'speed_zong', $param['id'], '总进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SpeedZongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = SpeedZongListModel::where(['zongid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('speed_zong', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_speed_zong(Request $request){
        $param = $request->post();
        $data = SpeedZongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            SpeedZongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new EnginRelevan())->vate_engin_file('speed_zong', $data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'speed_zong', $data['id'], '总进度计划');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_speed_zong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SpeedZongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = SpeedZongListModel::where(['zongid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('speed_zong', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_speed_month(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['month.deleted_user','=', 0];
        $where[] = ['month.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['month.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['month.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['month.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['month.month','=', $param['month']];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['month.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['month.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedMonthModel::alias('month')
            ->field('month.*,e.name as enginname')
            ->join('engin e','e.id=month.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_month', 'month'))
            ->order('month.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('speed_month');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_speed_month(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入月计划名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择计划月份');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            if((new SceneService())->check_speed_month_name($param['name'], $param['month'], $param['enginid'])){
                return outputJson(4000, '该月份内月计划名称已存在');
            }


            $param['number'] = (new BianhaoService())->create_speed_month_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'speed_month') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }
            Db::startTrans();
            try {
                $id = SpeedMonthModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['monthid'] = $id;
                    SpeedMonthListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_month',$id);
                (new EnginRelevan())->vate_engin_file('speed_month', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'speed_month', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'speed_month', $id, '月进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new BianhaoService())->create_speed_month_number();
            $data['files'] = (new OptService())->get_file('speed_month');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_speed_month(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SpeedMonthModel::where(['id'=>$param['id']])->find();


            if(empty($param['name'])){
                return outputJson(4000, '请输入月计划名称');
            }
            if(empty($param['month'])){
                return outputJson(4000, '请选择计划月份');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入编制人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择编制日期');
            }

            if((new SceneService())->check_speed_month_name($param['name'], $param['month'], $old['enginid'], $old['id'])){
                return outputJson(4000, '该月份内月计划名称已存在');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'speed_month') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入计划明细');
            }

            Db::startTrans();
            try {
                SpeedMonthModel::update($param);

                SpeedMonthListModel::where(['monthid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['monthid'] = $param['id'];
                    SpeedMonthListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'speed_month',$param['id']);
                (new EnginRelevan())->vate_engin_file('speed_month', $param['id']);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'speed_month', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'speed_month', $param['id'], '月进度计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SpeedMonthModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $list = SpeedMonthListModel::where(['monthid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('speed_month', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_speed_month(Request $request){
        $param = $request->post();
        $data = SpeedMonthModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            SpeedMonthModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new EnginRelevan())->vate_engin_file('speed_month', $data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'speed_month', $data['id'], '月进度计划');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_speed_month(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SpeedMonthModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $list = SpeedMonthListModel::where(['monthid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('speed_month', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_diary(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['diary.deleted_user','=', 0];
        $where[] = ['diary.comid','=',$this->userinfo['comid']];
        $where[] = ['diary.isturn','=',1];
        $where[] = ['diary.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['diary.enginid','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
//        if(isset($param['enginname']) && !empty($param['enginname'])){
//            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
//        }
//        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
//            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
//        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['diary.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['tian']) && !empty($param['tian'])){
            $where[] = ['diary.tian','like', '%'.$param['tian'].'%'];
        }
        if(isset($param['wen']) && !empty($param['wen'])){
            $where[] = ['diary.wen','like', '%'.$param['wen'].'%'];
        }
        if(isset($param['feng']) && !empty($param['feng'])){
            $where[] = ['diary.feng','like', '%'.$param['feng'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['diary.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['jier']) && !empty($param['jier'])){
            $where[] = ['diary.jier','like', '%'.$param['jier'].'%'];
        }
        if(isset($param['tianer']) && !empty($param['tianer'])){
            $where[] = ['diary.tianer','like', '%'.$param['tianer'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['diary.status','=', $param['status']];
        }

        $data['data'] = EnginDiaryModel::alias('diary')
            ->field('diary.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=diary.enginid')
            ->leftJoin('flow_bill bill','bill.id=diary.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_diary', 'e'))
            ->order('diary.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_diary');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_diary(Request $request){
        $param = $request->post();
        $data = EnginDiaryModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginDiaryModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_diary', $data['id'], '施工日志');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_diary(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginDiaryModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $data['files'] = (new OptService())->get_file('engin_diary', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_speed(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['speed.deleted_user','=', 0];
        $where[] = ['speed.comid','=',$this->userinfo['comid']];
        $where[] = ['speed.isturn','=',1];
        $where[] = ['speed.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['speed.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['speed.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['speed.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['speed.month','like', '%'.$param['month'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['speed.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginSpeedModel::alias('speed')
            ->field('speed.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=speed.enginid')
            ->leftJoin('flow_bill bill','bill.id=speed.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_speed', 'speed'))
            ->order('speed.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_speed');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_speed(Request $request){
        $param = $request->post();
        $data = EnginSpeedModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginSpeedModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_speed', $data['id'], '进度填报');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_speed(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginSpeedModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['monthinfo'] = SpeedMonthModel::where(['id'=>$data['data']['monthid']])->find();


        $list = EnginSpeedListModel::where(['speedid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['zhanbi'] = 0;
            if($item['finnum'] != 0 && $item['shinum'] != 0){
                $item['zhanbi'] = round($item['shinum']/$item['finnum']*100, 2);
            }
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('engin_speed', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_speed_jingen(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['month.deleted_user','=', 0];
        $where[] = ['month.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['month.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['monthname']) && !empty($param['monthname'])){
            $where[] = ['month.name','like', '%'.$param['monthname'].'%'];
        }
        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['list.code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['list.buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['list.xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        if(!isset($param['month']) || empty($param['month'])){
            $seamonth = date("Y-m", strtotime('-1 month')).' - '. date("Y-m", strtotime('-1 month'));
        }else{
            $seamonth = $param['month'];
        }


        $where[] = ['month.month','between', explode(' - ', $seamonth)];

        $data['is_month'] = $seamonth;


        $data['data'] = SpeedMonthListModel::alias('list')
            ->field('list.*,e.name as enginname,month.month,month.name as monthname')
            ->join('speed_month month','month.id=list.monthid')
            ->join('engin e','e.id=month.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('speed_jingen', 'month'))
            ->order('month.enginid DESC, month.month DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['benshinum'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1,'list.monthid'=>$item['id']])
                    ->sum('shinum');
                $item['benshiday'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1,'list.monthid'=>$item['id']])
                    ->sum('shiday');
                $item['zhanbi'] = 0;
                $item['sname'] = false;
                if($item['finnum'] != 0){
                    $item['zhanbi'] = round($item['benshinum']/$item['finnum']*100,2);
                }
                if($item['zhanbi'] >= 100){
                    $item['sname'] = true;
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_speed_jinhui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zong.deleted_user','=', 0];
        $where[] = ['zong.comid','=',$this->userinfo['comid']];



        $enginid = 0;
        if(!isset($param['enginid']) || empty($param['enginid'])){
            $enginid = EnginSpeedModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'comid'=>$this->userinfo['id']])
                ->order('month DESC')->value('enginid');
        }else{
            $enginid = $param['enginid'];
        }
        $where[] = ['zong.enginid','=',$enginid];
//
//        $data['enginidArr'] = EnginModel::field('id as value,name')
//            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])->select()->toArray();
//        $data['is_enginid'] = $enginid;

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['list.code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['list.buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['list.xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }




        $data['data'] = SpeedZongListModel::alias('list')
            ->field('list.*,e.name as enginname,zong.enginid')
            ->join('speed_zong zong','zong.id=list.zongid')
            ->join('engin e','e.id=zong.enginid')
            ->where((new MenuService())->get_miss_where('speed_jinhui', 'zong'))
            ->where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shifindt'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->value('findt');

                $item['leishinum'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->sum('shinum');

                $item['leishichan'] = round($item['leishinum']*$item['price'], 2);
                $item['leishiday'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->sum('shiday');

                $item['dangjin'] = 0;
                if($item['num'] != 0){
                    $item['dangjin'] = round($item['leishinum']/$item['num']*100, 2);
                }

                $item['gongbi'] = 0;
                if($item['duration'] != 0){
                    $item['gongbi'] = round($item['leishiday']/$item['duration']*100, 2);
                }

                $item['gongjing'] = false;
                $dangjin = $item['dangjin'] > 100 ? 100 : $item['dangjin'];
                $gongbi = $item['gongbi'] > 100 ? 100 : $item['gongbi'];
                if($dangjin >= $gongbi){
                    $item['gongjing'] = true;
                }
                $item['gongjing_str'] = $item['gongjing'] ? '正常' : '滞后';

                $item['dainum'] = round($item['num']-$item['leishinum'], 3);
                $item['dainum'] = $item['dainum'] < 0 ? 0 : $item['dainum'];

                $item['daichan'] = round($item['chanfee']-$item['leishichan'], 2);
                $item['daichan'] = $item['daichan'] < 0 ? 0 : $item['daichan'];

                $item['sheng'] = $item['duration']-$item['leishiday'];

                $item['zuijin'] = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$item['enginid'],'list.zongid'=>$item['id']])
                    ->order('speed.month DESC')
                    ->value('speed.month');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_engin_jian(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jian.deleted_user','=', 0];
        $where[] = ['jian.comid','=',$this->userinfo['comid']];



        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['jian.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['jiantype']) && !empty($param['jiantype'])){
            $where[] = ['jian.jiantype','=', $param['jiantype']];
        }


        $data['data'] = EnginJianModel::alias('jian')
            ->field('jian.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_jian', 'jian'))
            ->order('jian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['jiantype'] = (new OptService())->get_base_showname('scene_jiantype', $item['jiantype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_jian');
        $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_jian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['jiantype'])){
                return outputJson(4000, '请选择检查项类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入检查项');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_jian') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginJianModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_jian',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_jian', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_jian', $id, '检查项维护');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
            $data['files'] = (new OptService())->get_file('engin_jian');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_jian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginJianModel::where(['id'=>$param['id']])->find();

            if(empty($param['jiantype'])){
                return outputJson(4000, '请选择检查项类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入检查项');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_jian') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);


            Db::startTrans();
            try {
                EnginJianModel::update($param);
                (new OptService())->relation_file($files,'engin_jian',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_jian', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_jian', $param['id'], '检查项维护');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginJianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
            $data['files'] = (new OptService())->get_file('engin_jian', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_jian(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['id']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择需要删除的数据');
        }

        $data = EnginJianModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            foreach ($ids as $id){
                $data = EnginJianModel::where(['id'=>$id,'comid'=>$this->userinfo['comid']])->find();
                if(!$data){
                    continue;
                }
                EnginJianModel::where(['id' => $data['id']])->update([
                    'deleted_user' => $this->userinfo['id'],
                    'deleted_at' => date('Y-m-d H:i:s')
                ]);
                (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_jian', $data['id'], '检查项维护');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_jian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginJianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['jiantype'] = (new OptService())->get_base_showname('scene_jiantype', $data['data']['jiantype']);
        $data['files'] = (new OptService())->get_file('engin_jian', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_xun(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['xun.deleted_user','=', 0];
        $where[] = ['xun.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['xun.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xun.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['xun.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xuntype']) && !empty($param['xuntype'])){
            $where[] = ['xun.xuntype','=', $param['xuntype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['xun.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $wherexundept = '';
        if(isset($param['xundept']) && !empty($param['xundept'])){
            $wherexundept = Db::raw("find_in_set(".$param['xundept'].",xundept)");
        }

        $wherexuner = '';
        if(isset($param['xuner']) && !empty($param['xuner'])){
            $wherexuner = Db::raw("find_in_set(".$param['xuner'].",xuner)");
        }

        $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
        $data['is_xuntype'] = isset($param['xuntype']) ? $param['xuntype'] : '';

        $data['data'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->where((new MenuService())->get_miss_where('engin_xun', 'xun'))
            ->order('xun.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['xuntype'] = (new OptService())->get_base_showname('scene_xuntype', $item['xuntype']);
                $item['xundept_name'] = DeptModel::get_name_by_ids($item['xundept']);
                $item['xuner_name'] = UserModel::get_realname_by_ids($item['xuner']);


                $zheng_number = EnginZhengModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'xunid'=>$item['id']])
                    ->column('number');
                $item['zheng_number'] = implode(',',$zheng_number);
                $item['zhengnum'] = count($zheng_number);
                return $item;
            })->toArray();


        $data['all'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->where((new MenuService())->get_miss_where('engin_xun', 'xun'))
            ->count();
        $data['toyear'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->where('xun.dt', 'egt', date("Y-01-01"))
            ->where((new MenuService())->get_miss_where('engin_xun', 'xun'))
            ->count();

        $data['butauth'] = (new MenuService())->get_butauth('engin_xun');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_xun(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入巡检名称');
            }
            if(empty($param['xuntype'])){
                return outputJson(4000, '请选择巡检类型');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择巡检日期');
            }

            $param['number'] = (new BianhaoService())->create_engin_xun_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $istong = $param['istong'];
            unset($param['istong']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入存在问题');
            }

            Db::startTrans();
            try {
                $id = EnginXunModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['xunid'] = $id;
                    EnginXunListModel::insert($vv);
                }

                if($istong > 0){
                    $enginname = EnginModel::where(['id'=>$param['enginid']])->value('name');
                    $xuntype = (new OptService())->get_base_showname('scene_xuntype', $param['xuntype']);
                    $mess = "巡检报告-".$enginname."-".$xuntype."-".$param['number'];
                    (new OptService())->add_todo($param['tonger'], '巡检报告',$mess, $id, 'engin_xun');
                }


                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_xun', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_xun', $id, '质安巡检报告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['number'] = (new BianhaoService())->create_engin_xun_number();
            $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');

            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_xun(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginXunModel::where(['id'=>$param['id']])->find();

            if(empty($param['name'])){
                return outputJson(4000, '请输入巡检名称');
            }
            if(empty($param['xuntype'])){
                return outputJson(4000, '请选择巡检类型');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择巡检日期');
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $istong = $param['istong'];
            unset($param['istong']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入存在问题');
            }

            Db::startTrans();
            try {
                EnginXunModel::update($param);

                EnginXunListModel::where(['xunid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['xunid'] = $param['id'];
                    EnginXunListModel::insert($vv);
                }

                if($istong > 0){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $xuntype = (new OptService())->get_base_showname('scene_xuntype', $param['xuntype']);
                    $mess = "巡检报告-".$enginname."-".$xuntype."-".$old['number'];
                    (new OptService())->add_todo($param['tonger'], '巡检报告',$mess, $param['id'], 'engin_xun');
                }

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_xun', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_xun', $param['id'], '质安巡检报告');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginXunModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['xundept_info'] = (new UserService())->get_users_part_arr($data['data']['xundept'], 'dept');
            $data['data']['xuner_info'] = (new UserService())->get_users_part_arr($data['data']['xuner'], 'user');
            $data['data']['tonger_info'] = (new UserService())->get_users_part_arr($data['data']['tonger'], 'user');


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;

            $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
            $data['level'] = (new SceneService())->get_engin_xun_level();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_xun(Request $request){
        $param = $request->post();
        $data = EnginXunModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginXunModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_xun', $data['id'], '质安巡检报告');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_xun(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginXunModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['xuntype'] = (new OptService())->get_base_showname('scene_xuntype', $data['data']['xuntype']);
        $data['data']['xundept_name'] = DeptModel::get_name_by_ids($data['data']['xundept']);
        $data['data']['xuner_name'] = UserModel::get_realname_by_ids($data['data']['xuner']);
        $data['data']['tonger_name'] = UserModel::get_realname_by_ids($data['data']['tonger']);


        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['cusname'] = count($item['filearr']);
        }
        $data['list'] = $list;
        $data['files'] = (new OptService())->get_file('engin_xun', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_engin_zheng(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zheng.deleted_user','=', 0];
        $where[] = ['zheng.comid','=',$this->userinfo['comid']];

        if(isset($param['zhengtype']) && !empty($param['zhengtype'])){
            if($param['zhengtype'] == 'um'){
                $where[] = ['zheng.gaista','=', 1];
            }elseif($param['zhengtype'] == 'no'){
                $where[] = ['zheng.gaista','=', 0];
            }elseif($param['zhengtype'] == 'yu'){
                $where[] = ['zheng.gaista','=', 0];
                $where[] = ['zheng.dt','<', date("Y-m-d")];
            }
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['xunid']) && !empty($param['xunid'])){
            $where[] = ['zheng.xunid','=', $param['xunid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zheng.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['xunnumber']) && !empty($param['xunnumber'])){
            $where[] = ['xun.number','like', '%'.$param['xunnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['zheng.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['zheng.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->order('zheng.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                $item['fuzeer_name'] = UserModel::get_realname_by_ids($item['fuzeer']);
                $item['gaista_name'] = (new SceneService())->get_zheng_gaista_name($item['gaista'], $item['dt']);
                $item['fusta_name'] = (new SceneService())->get_zheng_fusta_name($item['fusta']);

                $item['is_fuzeer'] = in_array($this->userinfo['id'], explode(',', $item['fuzeer'])) ? true : false;
                $item['is_fujianer'] = $this->userinfo['id'] == $item['fujianer'] ? true : false;
                return $item;
            })->toArray();



        $data['all'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->count();

        $data['zhengn_um'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->where('zheng.gaista', '=', 1)
            ->count();

        $data['zhengnum_no'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->where('zheng.gaista', '=', 0)
            ->count();

        $data['zhengnum_yu'] = EnginZhengModel::alias('zheng')
            ->field('zheng.*,e.name as enginname,xun.number as xunnumber')
            ->join('engin e','e.id=zheng.enginid')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_zheng', 'zheng'))
            ->where('zheng.dt', '<', date("Y-m-d"))
            ->where('zheng.gaista', '=', 0)
            ->count();


        $data['butauth'] = (new MenuService())->get_butauth('engin_zheng');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['fuzeer'])){
                return outputJson(4000, '请选择整改负责人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择要求整改期限');
            }
            if(empty($param['fujianer'])){
                return outputJson(4000, '请选择复检人');
            }


            $param['number'] = (new BianhaoService())->create_engin_zheng_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_zheng') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入需整改的问题');
            }

            Db::startTrans();
            try {
                $id = EnginZhengModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhengid'] = $id;
                    EnginZhengListModel::insert($vv);
                }


                $enginname = EnginModel::where(['id'=>$param['enginid']])->value('name');
                $fuzeerName = UserModel::get_realname_by_ids($param['fuzeer']);
                $mess = "整改通知-".$enginname."-".$param['number']."-".$fuzeerName;
                (new OptService())->add_todo($param['fuzeer'], '整改通知',$mess, $id, 'engin_zheng');


                (new OptService())->relation_file($files,'engin_zheng',$id);
                (new EnginRelevan())->vate_engin_file('engin_zheng', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_zheng', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_zheng', $id, '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new BianhaoService())->create_engin_zheng_number();
            $data['files'] = (new OptService())->get_file('engin_zheng');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function wai_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['fuzeer'])){
                return outputJson(4000, '请选择整改负责人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择要求整改期限');
            }
            if(empty($param['fujianer'])){
                return outputJson(4000, '请选择复检人');
            }


            $param['number'] = (new BianhaoService())->create_engin_zheng_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_zheng') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入需整改的问题');
            }

            Db::startTrans();
            try {
                $id = EnginZhengModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhengid'] = $id;
                    EnginZhengListModel::insert($vv);
                }

                $enginname = EnginModel::where(['id'=>$param['enginid']])->value('name');
                $fuzeerName = UserModel::get_realname_by_ids($param['fuzeer']);
                $mess = "整改通知-".$enginname."-".$param['number']."-".$fuzeerName;
                (new OptService())->add_todo($param['fuzeer'], '整改通知',$mess, $id, 'engin_zheng');

                (new OptService())->relation_file($files,'engin_zheng',$id);
                (new EnginRelevan())->vate_engin_file('engin_zheng', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_zheng', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_zheng', $id, '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginXunModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['xunidinfo'] = $data['data'];



            $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])
                ->where(['iszheng'=>'是'])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['number'] = (new BianhaoService())->create_engin_zheng_number();
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginZhengModel::where(['id'=>$param['id']])->find();

            if($old['fusta'] > 0){
                return outputJson(4000, '已经整改过，不能编辑');
            }

            if(empty($param['fuzeer'])){
                return outputJson(4000, '请选择整改负责人');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择要求整改期限');
            }
            if(empty($param['fujianer'])){
                return outputJson(4000, '请选择复检人');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_zheng') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入需整改的问题');
            }

            Db::startTrans();
            try {
                EnginZhengModel::update($param);

                EnginZhengListModel::where(['zhengid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhengid'] = $param['id'];
                    EnginZhengListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'engin_zheng',$param['id']);
                (new EnginRelevan())->vate_engin_file('engin_zheng', $param['id']);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_zheng', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_zheng', $param['id'], '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginZhengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $xunidinfo = EnginXunModel::get_info_by_pk($data['data']['xunid']);
            $data['xunidinfo'] = $xunidinfo;

            $data['data']['fuzeer_info'] = (new UserService())->get_users_part_arr($data['data']['fuzeer'], 'user');
            $data['data']['fujianer_info'] = (new UserService())->get_users_part_arr($data['data']['fujianer'], 'user');

            $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_zheng(Request $request){
        $param = $request->post();
        $data = EnginZhengModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginZhengModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new EnginRelevan())->vate_engin_file('engin_zheng', $data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_zheng', $data['id'], '整改通知');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_zheng(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginZhengModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['deptid_name'] = DeptModel::get_name_by_pk($data['data']['deptid']);
        $data['data']['gaista_name'] = (new SceneService())->get_zheng_gaista_name($data['data']['gaista'],$data['data']['dt']);
        $data['data']['fusta_name'] = (new SceneService())->get_zheng_fusta_name($data['data']['fusta']);



        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['xunidinfo'] = EnginXunModel::get_info_by_pk($data['data']['xunid']);

        $data['data']['fuzeer_name'] = UserModel::get_realname_by_ids($data['data']['fuzeer']);
        $data['data']['fujianer_name'] = UserModel::get_realname_by_pk($data['data']['fujianer']);

        $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['cusname'] = count($item['filearr']);

            $item['gaifilearr'] = FileModel::where('id','in',$item['gaifiles'])->select()->toArray();
            $item['gaicusname'] = count($item['gaifilearr']);


            $item['files'] = $item['filearr'];
            $item['filesnum'] = count($item['files']);

            $item['gaifiles'] = $item['gaifilearr'];
            $item['gaifilesnum'] = count($item['gaifiles']);
        }
        $data['list'] = $list;


        $data['data']['is_fuzeer'] = in_array($this->userinfo['id'], explode(',', $data['data']['fuzeer'])) ? true : false;
        $data['data']['is_fujianer'] = $this->userinfo['id'] == $data['data']['fujianer'] ? true : false;
        $data['butauth'] = (new MenuService())->get_butauth('engin_zheng');
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gai_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginZhengModel::where(['id'=>$param['id']])->find();


            if(empty($param['gaidt'])){
                return outputJson(4000, '请选择整改完成时间');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_zheng') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入整改回复信息');
            }

            $param['gaista'] = 1;
            $param['fusta'] = 1;
            Db::startTrans();
            try {
                EnginZhengModel::update($param);

                foreach($list as $vv){
                    EnginZhengListModel::update($vv);
                }

                $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                $mess = "整改回复-".$enginname."-".$old['number'];
                (new OptService())->add_todo($old['fujianer'], '整改回复',$mess, $old['id'], 'engin_zheng');


                (new OptService())->relation_file($files,'engin_zheng',$param['id']);
                (new EnginRelevan())->vate_engin_file('engin_zheng', $param['id']);

                (new OptService())->add_flow_log('整改回复', 'engin_zheng', $param['id']);
                (new OptService())->add_log_list('整改回复', 'engin_zheng', $param['id'], '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginZhengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['fuzeer_name'] = UserModel::get_realname_by_ids($data['data']['fuzeer']);

            $gaiWhere = [];
            if($data['data']['gaista'] == 0 && $data['data']['fusta'] == 3){
                $gaiWhere[] = ['fuguo', '=', '不通过'];
            }

            $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->where($gaiWhere)->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = $item['filearr'];
                $item['filesnum'] = count($item['files']);

                $item['gaifilearr'] = FileModel::where('id','in',$item['gaifiles'])->select()->toArray();
                $item['gaifiles'] = $item['gaifilearr'];
                $item['gaifilesnum'] = count($item['gaifiles']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fu_engin_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = EnginZhengModel::where(['id'=>$param['id']])->find();


            if(empty($param['fudt'])){
                return outputJson(4000, '请选择复检时间');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'engin_zheng') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入整改回复信息');
            }




            $allfuguo = true;
            foreach ($list as $v){
                if($v['fuguo'] == '不通过'){
                    $allfuguo = false;
                }
            }

            if($allfuguo){
                $param['gaista'] = 1;
                $param['fusta'] = 2;
            }else{
                $param['gaista'] = 0;
                $param['fusta'] = 3;
            }

            Db::startTrans();
            try {
                EnginZhengModel::update($param);

                foreach($list as $vv){
                    EnginZhengListModel::update($vv);
                }

                if($param['fusta'] == 2){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $mess = "复检验收-".$enginname."-".$old['number']. ' - 复检通过';
                    (new OptService())->add_todo($old['fuzeer'], '复检验收',$mess, $old['id'], 'engin_zheng');
                }
                if($param['fusta'] == 3){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $mess = "复检验收-".$enginname."-".$old['number']. ' - 复检未通过，部分检查项需重新整改';;
                    (new OptService())->add_todo($old['fuzeer'], '复检验收',$mess, $old['id'], 'engin_zheng');
                }

                (new OptService())->relation_file($files,'engin_zheng',$param['id']);
                (new EnginRelevan())->vate_engin_file('engin_zheng', $param['id']);

                (new OptService())->add_flow_log('复检验收', 'engin_zheng', $param['id']);
                (new OptService())->add_log_list('复检验收', 'engin_zheng', $param['id'], '整改通知');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginZhengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['fuzeer_name'] = UserModel::get_realname_by_ids($data['data']['fuzeer']);

            $list = EnginZhengListModel::where(['zhengid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = $item['filearr'];
                $item['filesnum'] = count($item['files']);

                $item['gaifilearr'] = FileModel::where('id','in',$item['gaifiles'])->select()->toArray();
                $item['gaifiles'] = $item['gaifilearr'];
                $item['gaifilesnum'] = count($item['gaifiles']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('engin_zheng', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function detail_engin_stamp(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['stamp.deleted_user','=', 0];
        $where[] = ['stamp.comid','=',$this->userinfo['comid']];
        $where[] = ['stamp.isturn','=',1];
        $where[] = ['stamp.isgai','=',1];
        $where[] = ['stamp.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['stamp.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['zitype']) && !empty($param['zitype'])){
            $where[] = ['stamp.zitype','=', $param['zitype']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['stamp.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['stamp.flowdept','like', '%'.$param['flowdept'].'%'];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['list.name','like', '%'.$param['name'].'%'];
        }

        $whereYongtype = '';
        if(isset($param['yongtype']) && !empty($param['yongtype'])){
            $whereYongtype = Db::raw("find_in_set(".$param['yongtype'].",yongtype)");
        }

        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['list.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['isfan']) && !empty($param['isfan'])){
            $where[] = ['list.isfan','=', $param['isfan']];
        }
        if(isset($param['gaidt']) && !empty($param['gaidt'])){
            $where[] = ['list.gaidt','between', explode(' - ', $param['gaidt'])];
        }
        if(isset($param['gaier']) && !empty($param['gaier'])){
            $where[] = ['list.gaier','=', $param['gaier']];
        }
        if(isset($param['fandt']) && !empty($param['fandt'])){
            $where[] = ['list.fandt','between', explode(' - ', $param['fandt'])];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['list.jieer','like', '%'.$param['jieer'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
        $data['is_zitype'] = isset($param['zitype']) ? $param['zitype'] : '';

        $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
        $data['is_yongtype'] = isset($param['yongtype']) ? $param['yongtype'] : '';

        $data['data'] = EnginStampListModel::alias('list')
            ->field('list.*,e.name as enginname,stamp.zitype,stamp.flowuser,stamp.flowdept,stamp.created_at')
            ->join('engin_stamp stamp','stamp.id=list.stampid')
            ->join('engin e','e.id=stamp.enginid')
            ->where($where)
            ->where($whereYongtype)
            ->where((new MenuService())->get_miss_where('engin_stamp_detail', 'stamp'))
            ->order('stamp.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zitype'] = (new OptService())->get_base_showname('scene_zitype', $item['zitype']);
                $item['yongtype'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_stamp_detail', 1);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_stamp(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['stamp.deleted_user','=', 0];
        $where[] = ['stamp.comid','=',$this->userinfo['comid']];
        $where[] = ['stamp.isturn','=',1];
        $where[] = ['stamp.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['stamp.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['zitype']) && !empty($param['zitype'])){
            $where[] = ['stamp.zitype','=', $param['zitype']];
        }
        if(isset($param['isgai']) && $param['isgai'] != ''){
            $where[] = ['stamp.isgai','=', $param['isgai']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['stamp.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['stamp.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['stamp.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
        $data['is_zitype'] = isset($param['zitype']) ? $param['zitype'] : '';

        $data['data'] = EnginStampModel::alias('stamp')
            ->field('stamp.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=stamp.enginid')
            ->leftJoin('flow_bill bill','bill.id=stamp.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_stamp', 'stamp'))
            ->order('stamp.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zitype'] = (new OptService())->get_base_showname('scene_zitype', $item['zitype']);
                $item['isgai_name'] = $item['isgai'] > 0 ? '已盖章' : '未盖章';
                $item['nums'] = EnginStampListModel::where(['stampid'=>$item['id']])->sum('num');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_stamp');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_engin_stamp(Request $request){
        $param = $request->post();
        $data = EnginStampModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            EnginStampModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_stamp', $data['id'], '项目资料盖章');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_stamp(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginStampModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['zitype'] = (new OptService())->get_base_showname('scene_zitype', $data['data']['zitype']);


        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
            $item['yongtype'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
            $item['cusname'] = count($item['filearr']);
        }
        $data['list'] = $list;
        $data['files'] = (new OptService())->get_file('engin_stamp', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gai_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            Db::startTrans();
            try {
                EnginStampModel::where(['id'=>$param['id']])->update(['isgai'=>1]);
                foreach($list as $vv){
                    EnginStampListModel::update($vv);
                }

                (new OptService())->add_flow_log('盖章', 'engin_stamp', $param['id']);
                (new OptService())->add_log_list('盖章', 'engin_stamp', $param['id'], '项目资料盖章');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginStampModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['yongtype'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                if($data['data']['isgai'] < 1){
                    $item['gaidt'] = date("Y-m-d");
                    $item['gaier'] = $this->userinfo['realname'];
                }

                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fan_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入盖章资料明细');
            }

            Db::startTrans();
            try {
                foreach($list as $vv){
                    EnginStampListModel::update($vv);
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $ids = $request->get('ids',0,'string');

            $list = EnginStampListModel::where('id','in',$ids)->where(['isfan'=>'是'])->select()->toArray();
            foreach ($list as &$item){
                $stamp = EnginStampModel::where(['id'=>$item['stampid']])->find();
                $item['shener'] = $stamp['flowuser'];
                $item['enginname'] = EnginModel::where(['id'=>$stamp['enginid']])->value('name');

                if(empty($item['fandt'])){
                    $item['fandt'] = date("Y-m-d");
                }
                if(empty($item['jieer'])){
                    $item['jieer'] = $this->userinfo['realname'];
                }

                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function index_engin_fold(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.state','in',[3,7,9]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.id','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.id,e.name,e.state,e.status,e.business,e.chanid,e.number')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['filesnum'] = EnginFoldModel::where(['enginid'=>$item['id'],'isdel'=>0,'deleted_user'=>0])->count();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_fold(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['f.comid','=',$this->userinfo['comid']];
        $where[] = ['f.deleted_user','=', 0];
        $where[] = ['f.isdel','=', 0];
        $where[] = ['f.enginid','=', $param['enginid']];

        $data['enginname'] = EnginModel::where(['id'=>$param['enginid']])->value('name');

        if(isset($param['muid']) && !empty($param['muid'])){
            $where[] = ['f.muid','=', $param['muid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['f.name|f.typename|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['f.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['typename']) && !empty($param['typename'])){
            $where[] = ['f.typename','like', '%'.$param['typename'].'%'];
        }
        if(isset($param['realname']) && !empty($param['realname'])){
            $where[] = ['user.realname','like', '%'.$param['realname'].'%'];
        }
        if(isset($param['table']) && !empty($param['table'])){
            if($param['table'] == 'kong'){
                $where[] = ['f.table','=', ''];
            }else{
                $where[] = ['f.table','=', $param['table']];
            }
        }


        $tableArr = [];
        $tableArr['kong'] = '空白';
        $fold_table = (new EnginService())->get_engin_fold_table();
        foreach ($fold_table as $kkkkk=>$vvvvv){
            $tableArr[$kkkkk] = $vvvvv;
        }

        $data['table'] = $tableArr;
        $data['is_table'] = isset($param['table']) ? $param['table'] : '';


        $data['data'] = EnginFoldModel::alias('f')
            ->field('f.*,user.realname as created_user_name')
            ->leftJoin('user user','user.id=f.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_fold', 'f'))
            ->order('f.created_at DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['muid_name'] = (new OptService())->get_base_daotopname('engin_fold_muid', $item['muid']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_fold');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function zhuan_engin_fold(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['muid'])){
                return outputJson(4000, '请选择要转入的文件夹');
            }
            unset($param['token']);

            Db::startTrans();
            try {
                $idsArr = explode(',', $param['ids']);
                foreach ($idsArr as $foldid){
                    EnginFoldModel::where(['id'=>$foldid])->update(['muid'=>$param['muid']]);
                }
                EnginFoldModel::where('id','in', $param['ids'])->update(['muid'=>$param['muid']]);

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['muid'] = (new OptService())->get_base_info('engin_fold_muid');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_fold(Request $request){
        $param = $request->post();
        $data = EnginFoldModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            //进入回收站
            if($param['type'] == 1){
                EnginFoldModel::where(['id'=>$data['id']])->update(['isdel'=>1,'deldt'=>date("Y-m-d H:i:s")]);
            }

            //回收站里面进行时删除
            if($param['type'] == 2){
                EnginFoldModel::where(['id'=>$data['id']])->update(['isdel'=>1, 'deleted_user'=>$this->userinfo['id'],'deleted_at'=>date("Y-m-d H:i:s")]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function shou_engin_fold(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['f.comid','=',$this->userinfo['comid']];
        $where[] = ['f.deleted_user','=', 0];
        $where[] = ['f.isdel','>', 0];
        $where[] = ['f.enginid','=', $param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['f.name|f.typename|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['f.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['typename']) && !empty($param['typename'])){
            $where[] = ['f.typename','like', '%'.$param['typename'].'%'];
        }
        if(isset($param['realname']) && !empty($param['realname'])){
            $where[] = ['user.realname','like', '%'.$param['realname'].'%'];
        }
        if(isset($param['table']) && !empty($param['table'])){
            $where[] = ['f.table','=', $param['table']];
        }

        $data['table'] = (new EnginService())->get_engin_fold_table();
        $data['is_table'] = isset($param['table']) ? $param['table'] : '';

        $data['data'] = EnginFoldModel::alias('f')
            ->field('f.*,user.realname as created_user_name')
            ->leftJoin('user user','user.id=f.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_fold', 'f'))
            ->order('f.created_at DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hui_engin_fold(Request $request){
        $param = $request->post();
        $data = EnginFoldModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            EnginFoldModel::where(['id'=>$data['id']])->update([
                'muid'=>0,
                'isdel'=>0,
                'deldt'=>'',
                'deleted_user'=>'',
                'deleted_at'=>'',
            ]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
























































}
